﻿using FishNet.Object;
using FishNet.Object.Synchronizing;
using GameBasic;
using Unity.Collections;
using UnityEngine;

namespace VCity
{
    public class NetPlayer : NetworkBehaviour
    {
        public static NetPlayer Instance;

        // skin
        [HideInInspector]
        [SyncVar]
        public int skin;

        // nickname
        [HideInInspector]
        [SyncVar]
        public string nickName;

        // animation
        [field: SyncVar(ReadPermissions = ReadPermission.ExcludeOwner)]
        public int Anim 
        { 
            get; 
            [ServerRpc(RunLocally = true)] 
            set;
        }
        int animCache;

        // other player's avatar
        NetCharCtrl avatar;

        // name, skin
        public override void OnStartClient()
        {
            base.OnStartClient();

            //Debug.Log($"OnStartClient {Owner.ClientId}");
            if (IsOwner)
            {
                Instance = this;
            }
            // other player
            else
            {
                // create avatar
                var gr = GameResources.Instance;
                //
                avatar = Instantiate(gr.netCharPrefab, transform);
                // set skin
                var meshes = gr.characterMeshes;
                int skinIndex = skin;
                if (meshes.IsLegalIndex(skinIndex))
                    avatar.meshObj.sharedMesh = meshes[skinIndex];
                // set name
                avatar.nameText.text = nickName;

                // Log
                //Debug.Log($"{nickName} joined");
            }
        }

        //public override void OnStopClient()
        //{
        //    base.OnStopClient();

        //    Debug.Log($"{nickName} left");
        //}

        private void Update()
        {
            if (IsClient)
            {
                if (IsOwner)
                {
                    // update transform
                    var charCtrl = ClientMain.Instance.charCtrl;
                    var ct = charCtrl.transform;
                    transform.position = ct.position;
                    transform.rotation = ct.rotation;

                    //// animation
                    if (animCache != charCtrl.CurrentAnimHash)
                    {
                        animCache = charCtrl.CurrentAnimHash;
                        Anim = animCache;
                    }
                }
                else
                {
                    // anim changed && same match
                    if (animCache != Anim && avatar != null)
                    {
                        animCache = Anim;
                        avatar.animator.Play(animCache);
                    }
                }
            }
        }

        public void OnDestroy()
        {
            //Debug.Log($"{OwnerClientId} despawned");

            //if (IsServer && NetServer.Instance != null)
            //{
            //    NetServer.Instance.OnDespawn(this);
            //}

            if (IsOwner)
            {
                Instance = null;
            }
        }
    }
}